#include "devwidget.h"
#include "readonlydelegate.h"
#include <stdio.h>


extern QList<DevConf> confDevList;
extern Channel chn[2];
extern bool parseDev(QString filepath);
extern bool parseDatamap();


DevWidget::DevWidget(QWidget *parent) :
    QWidget(parent)
{
    setupUi(this);
    parseDev("./config/dev.cfg");
    parseDatamap();
    mDevModel=new DevStandardItemModel;
    mDevModel->setDatas(confDevList);
    mDevTable->setModel(mDevModel);

    mDevTable->setItemDelegateForColumn(0,new readonlyDelegate(this));
    mDevTable->setItemDelegateForColumn(1,new readonlyDelegate(this));
    mDevTable->setItemDelegateForColumn(3,new readonlyDelegate(this));
    mDevTable->setItemDelegateForColumn(4,new readonlyDelegate(this));

    connect(mDevModel,SIGNAL(dataChanged(QModelIndex,QModelIndex)),this,SLOT(devNumChanged(QModelIndex,QModelIndex)));
    connect(nextBtn,SIGNAL(clicked()),this,SLOT(next()));

}

void DevWidget:: devNumChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
{
    int row=topLeft.row();
    int column=topLeft.column();
    QVariant val=topLeft.data();

    if(column==2)
    {
        confDevList[row].num=val.toInt();
        int chn1=0,chn2=0;
        if(confDevList[row].num%2==1)
        {
            chn1=(confDevList[row].num/2)+1;
            chn2=chn1-1;
        }
        else
        {
            chn1=chn2=(confDevList[row].num/2);
        }
        confDevList[row].chn1=chn1;
        confDevList[row].chn2=chn2;
        mDevModel->setItem(row, column+1, new QStandardItem(QString::number(chn1)));
        mDevModel->setItem(row, column+2, new QStandardItem(QString::number(chn2)));
    }

}

void DevWidget::next()
{
    chn[0].sysdev.clear();
    chn[1].sysdev.clear();
    for(int i=0;i<confDevList.size();i++)
    {
        if(confDevList.at(i).chn1>0)
        {
            for(int j=0;j<confDevList.at(i).chn1;j++)
            {
                DEV temp;
                temp.dev_name=confDevList.at(i).dev_name;
                temp.dev_type=confDevList.at(i).dev_type;
                temp.devAddr=0;
                chn[0].sysdev<<temp;
            }

        }

        if(confDevList.at(i).chn2>0)
        {
            for(int j=0;j<confDevList.at(i).chn2;j++)
            {
                DEV temp;
                temp.dev_name=confDevList.at(i).dev_name;
                temp.dev_type=confDevList.at(i).dev_type;
                temp.devAddr=0;
                chn[1].sysdev<<temp;
            }

        }
    }

    for(int j=0;j<confDevList.size();j++)
    {
        if(confDevList.at(j).num>confDevList.at(j).maxCount)
        {
            QString msg;
            msg=QString("设备类型:%1,已超过最大限制数:%2").arg(confDevList.at(j).dev_type).arg(confDevList.at(j).maxCount);
            QMessageBox::information(this,"提示",msg);
            return;
        }
    }
   emit mystep(1);
}




DevStandardItemModel::DevStandardItemModel(QObject *parent):QStandardItemModel(parent)
{
    this->initProperty();
    //this->setData();
}

DevStandardItemModel::~DevStandardItemModel()
{

}

void DevStandardItemModel::initProperty()
{
    this->setColumnCount(5);
    QStringList list;
    list<<"装置类型"<<"装置名称"<<"数量"<<"通道1"<<"通道2";
    this->setHorizontalHeaderLabels( list);
}

QVariant DevStandardItemModel::data(const QModelIndex & index,int role) const
{
    int column=index.column();
    int row=index.row();

    if(role==Qt::DisplayRole)
    {
        if(column==0)
            return datas.at(row).dev_type;
        if(column==1)
            return datas.at(row).dev_name;
        //return 0;
    }


    return QStandardItemModel::data(index,role);
}

void DevStandardItemModel::setDatas(QList<DevConf> datas)
{
    this->removeColumns(0,this->columnCount());
    this->setRowCount(datas.size());
    this->setColumnCount(5);
    this->datas=datas;
}

